package com.earl.eatbao.web;

import static org.slf4j.LoggerFactory.getLogger;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.earl.eatbao.commons.vo.ResultMessage;
import com.earl.eatbao.service.VerifyCodeService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value = "/verifycode")
//@Api(value="Person Rest Service") //修改生成的son路径
public class VerifyCodeController extends BaseController{

	// desc:日记工具logger
	private static final Logger logger = getLogger(VerifyCodeController.class);

	@Autowired
	private VerifyCodeService verifyCodeService;

	/**
	 * 生成验证码，一个用户只能有一个验证码，重复的画，会被覆盖
	 * GET /verifyCode -> get the verifyCode
	 */
	@RequestMapping(value = "/code/{phoneNumber}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "生成验证码", notes = "get verifyCode ",httpMethod="GET",response=String.class)
	public ResultMessage genVerifyCode( @PathVariable String phoneNumber){
		logger.info("进入controller层的getVerifyCode方法");
		String verifyCode = verifyCodeService.genVerifyCode(phoneNumber);
		ResultMessage result = new ResultMessage();
		result.setServiceResult(true);
		result.setResultInfo(verifyCode);
		return result;
	}

	/**
	 * GET /test -> test the verifyCode
	 */
	@RequestMapping(value = "/{verifyCode}/{phoneNumber}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "获取验证码", notes = "test verifyCode ",httpMethod="GET",response=String.class)
	public ResultMessage comfigVerifyCode( @PathVariable String verifyCode , @PathVariable String phoneNumber){
		Boolean get = verifyCodeService.confirmVerifyCode(verifyCode,phoneNumber);
		ResultMessage result = new ResultMessage();
		result.setServiceResult(get);
		return result;
	}

	@ApiOperation(value = "获取手机验证码", notes = "手机号", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/getCode",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> getPhoneCode(
			@ApiParam(name = "contact", required = true, value = "获取验证码的手机号")
			@RequestParam(name = "contact", required = true)String contact ){

        logger.info("<====== 进入getPhoneCode:方法,{[contact] :" + contact +" } ====");
        // desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(verifyCodeService.getPhoneCode(contact));
		result.setMessageCode(200l);
		if (result.getServiceResult()) {
		    result.setResultInfo("请求操作成功");
		} else {
		    result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出getPhoneCode方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

}
